﻿using B.S.Traceability.Domain;
using B.S.Traceability.Error;
using B.S.Traceability.Infrastructure.Interface;
using B.S.Traceability.Writer.API.Application.Commends.Imgs;
using MediatR;

namespace B.S.Traceability.Writer.API.Application.CommendHandlers.Imgs
{
    public class DelInspecCommendHandlers : IRequestHandler<DelInspecCommends, APIResult<IntegrateImgs>>
    {


        private readonly IBaseRepository<IntegrateImgs> integrateRepository;

        public DelInspecCommendHandlers(IBaseRepository<IntegrateImgs> integrateRepository)
        {
            this.integrateRepository = integrateRepository;
        }

        /// <summary>
        /// 删除图片
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<IntegrateImgs>> Handle(DelInspecCommends request, CancellationToken cancellationToken)
        {
            APIResult<IntegrateImgs> result = new APIResult<IntegrateImgs>();
            result.Code = APIEnum.删除成功;
            result.Msg = "删除成功";

            var product = integrateRepository.GetAllAsync().Result.FirstOrDefault(p => p.IntegrateImgsId == request.IntegrateImgsId);
            product.IsDel = !product.IsDel;

            await integrateRepository.Update(product);
            return await Task.FromResult(result);
        }
    }
}
